package com.zy.mydemo.common.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.HashMap;

/**
 * @Author:zhongyong
 * @Description：
 * @Date：Create in 2020/04/29
 */

@Data
public class ResultUtils {
    private int code;
    private String message;
    private Object data;

    public ResultUtils(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultUtils(ResultTypeEnum resEnum) {
        this.code = resEnum.getCode();
        this.message = resEnum.getMessage();
    }

    public ResultUtils(ResultTypeEnum resEnum, String message) {
        this.code = resEnum.getCode();
        this.message = message;
    }


    public final static ResultUtils success() {
        return new ResultUtils(ResultTypeEnum.SUCCESS);
    }

    public final static ResultUtils success(String message) {
        return new ResultUtils(ResultTypeEnum.SUCCESS, message);
    }

    public final static ResultUtils success(String message, Object data) {
        ResultUtils result = new ResultUtils(ResultTypeEnum.SUCCESS, message);
        result.setData(data);
        return result;
    }

    public final static ResultUtils error() {
        return new ResultUtils(ResultTypeEnum.ERROR);
    }

    public final static ResultUtils error(String message) {
        return new ResultUtils(ResultTypeEnum.ERROR, message);
    }

    public final static ResultUtils error(ResultTypeEnum resEnum, String message) {
        return new ResultUtils(resEnum, message);
    }

    public final static ResultUtils error(int code, String message) {
        return new ResultUtils(code, message);
    }


    public final static ResultUtils sysError(String message) {
        return new ResultUtils(ResultTypeEnum.SYS_ERROR, message);
    }

    @Override
    public String toString() {
        HashMap<String, Object> hashMap = new HashMap() {{
            put("Code", getCode());
            put("Message", getMessage());
            if (data != null) {
                put("Data", getData());
            }
        }};
        return JSON.toJSONString(hashMap);
    }
}
